<%@tag description="pagination" pageEncoding="UTF-8"%>
<%@ attribute name="recordCount" type="java.lang.Integer" required="true"%>

<%
String pt = "pg=[0-9]+";
String url = request.getAttribute("javax.servlet.forward.request_uri").toString() 
           + "?" + request.getAttribute("javax.servlet.forward.query_string").toString();
Integer recordCount = (Integer)jspContext.getAttribute("recordCount");
int pageSize = Integer.parseInt(request.getParameter("sz"));
int pageCount = recordCount / pageSize;
if (pageCount * pageSize < recordCount) ++pageCount;
int currentPage = Integer.parseInt(request.getParameter("pg"));
if (currentPage > pageCount) currentPage = pageCount;
int base = ((currentPage - 1) / 10) * 10;
%>

<div class="pagination pagination-small pagination-centered">
  <ul>
    <% if (base > 0) { %>
      <li><a href="<%= url.replaceAll(pt, "pg=" + base) %>">&lt;</a></li>
    <% } %>

    <% for (int i=1; i<=10; ++i) { %>
      <% int n = base + i; 
         String s = (n == currentPage) ? "active" : ""; 
         if (n > pageCount) break; 
      %>
      <li class='<%= s %>'>
        <a href='<%= url.replaceAll(pt, "pg=" + n) %>' ><%=n%></a>
      </li>
    <% } %>
      
    <% if (base+11 <= pageCount) { %>
       <li><a href='<%= url.replaceAll(pt, "pg=" + (base+11)) %>'>&gt;</a></li>
    <% } %>
  </ul>
</div>
